package com.api.component;

import java.util.Calendar;
import java.util.Date;

import org.springframework.stereotype.Component;

import com.api.common.jwt.JwToken;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

/**
 * @ClassName: JwtComponent
 * @Description: jwt_TOKEN工具类
 */
@Component
public class JwtComponent {

	private static final String JWTSECRET = "Q951KBCNckeuY6RrafwDZpfHGoSX9Cvb";

	public JwToken createToken(String userInfo) {
		JwToken jwt = new JwToken();
		Calendar nowTime = Calendar.getInstance();
		nowTime.add(Calendar.HOUR_OF_DAY, 48);
		Date expiresDate = nowTime.getTime();
		String token = Jwts.builder().setSubject(userInfo).setExpiration(expiresDate)
				.setIssuedAt(Calendar.getInstance().getTime()).signWith(SignatureAlgorithm.HS512, JWTSECRET).compact();
		jwt.setToken(token);
		jwt.setExpiresDate(expiresDate);
		return jwt;
	}

	/**
	 * @Title: verifyToken @Description: 解析jwt @param @param
	 *         jwt @param @return @param @throws Exception @return String @throws
	 */
	public String verifyToken(String token) throws Exception {
		return Jwts.parser().setSigningKey(JWTSECRET).parseClaimsJws(token).getBody().getSubject();
	}
}
